共计 3545 个字符,预计需要花费 9 分钟才能阅读完成。
提醒:本文最后更新于 2024-08-30 15:29,文中所关联的信息可能已发生改变,请知悉!
项目源码
降噪部分的修改与运行
导入所需库
from __future__ import print_function
import matplotlib.pyplot as plt
%matplotlib inline
import os
#os.environ['CUDA_VISIBLE_DEVICES'] = '3'
import numpy as np
from models import *
import torch
import torch.optim
# from skimage.measure import compare_psnr
from skimage.metrics import peak_signal_noise_ratio as compare_psnr
from utils.denoising_utils import *
torch.backends.cudnn.enabled = True
torch.backends.cudnn.benchmark =True
dtype = torch.cuda.FloatTensor
imsize =-1
PLOT = True
sigma = 25
sigma_ = sigma/255.
在上述代码中,第十四行
from skimage.measure import compare_psnr
已经跟不上版本,不可用,所以我替换成下面的语句
from skimage.metrics import peak_signal_noise_ratio as compare_psnr
设置参数
INPUT = 'noise' # 'meshgrid'
pad = 'reflection'
OPT_OVER = 'net' # 'net,input'
reg_noise_std = 1./30. # set to 1./20. for sigma=50
LR = 0.01
OPTIMIZER='adam' # 'LBFGS'
show_every = 100
exp_weight=0.99
if fname == 'data/denoising/snail.jpg':
num_iter = 2400
input_depth = 3
figsize = 5
net = skip(
input_depth, 3,
num_channels_down = [8, 16, 32, 64, 128],
num_channels_up = [8, 16, 32, 64, 128],
num_channels_skip = [0, 0, 0, 4, 4],
upsample_mode='bilinear',
need_sigmoid=True, need_bias=True, pad=pad, act_fun='LeakyReLU')
net = net.type(dtype)
elif fname == 'data/denoising/F16_GT.png':
num_iter = 3000
input_depth = 32
figsize = 4
net = get_net(input_depth, 'skip', pad,
skip_n33d=128,
skip_n33u=128,
skip_n11=4,
num_scales=5,
upsample_mode='bilinear').type(dtype)
else:
assert False
net_input = get_noise(input_depth, INPUT, (img_pil.size[1], img_pil.size[0])).type(dtype).detach()
# Compute number of parameters
s = sum([np.prod(list(p.size())) for p in net.parameters()]);
print ('Number of params: %d' % s)
# Loss
mse = torch.nn.MSELoss().type(dtype)
img_noisy_torch = np_to_torch(img_noisy_np).type(dtype)
该部分会对测试图片进行噪声污染
但是,该部分仅针对“飞机”和“蜗牛”图片,普适性仍待考究
迭代优化
net_input_saved = net_input.detach().clone()
noise = net_input.detach().clone()
out_avg = None
last_net = None
psrn_noisy_last = 0
i = 0
def closure():
global i, out_avg, psrn_noisy_last, last_net, net_input
if reg_noise_std > 0:
net_input = net_input_saved + (noise.normal_() * reg_noise_std)
out = net(net_input)
# Smoothing
if out_avg is None:
out_avg = out.detach()
else:
out_avg = out_avg * exp_weight + out.detach() * (1 - exp_weight)
total_loss = mse(out, img_noisy_torch)
total_loss.backward()
psrn_noisy = compare_psnr(img_noisy_np, out.detach().cpu().numpy()[0])
psrn_gt = compare_psnr(img_np, out.detach().cpu().numpy()[0])
psrn_gt_sm = compare_psnr(img_np, out_avg.detach().cpu().numpy()[0])
# Note that we do not have GT for the "snail" example
# So 'PSRN_gt', 'PSNR_gt_sm' make no sense
print ('Iteration %05d Loss %f PSNR_noisy: %f PSRN_gt: %f PSNR_gt_sm: %f' % (i, total_loss.item(), psrn_noisy, psrn_gt, psrn_gt_sm), '\r', end='')
if PLOT and i % show_every == 0:
out_np = torch_to_np(out)
plot_image_grid([np.clip(out_np, 0, 1),
np.clip(torch_to_np(out_avg), 0, 1)], factor=figsize, nrow=1)
# Backtracking
if i % show_every:
if psrn_noisy - psrn_noisy_last < -5:
print('Falling back to previous checkpoint.')
for new_param, net_param in zip(last_net, net.parameters()):
net_param.data.copy_(new_param.cuda())
return total_loss*0
else:
last_net = [x.detach().cpu() for x in net.parameters()]
psrn_noisy_last = psrn_noisy
i += 1
return total_loss
p = get_params(OPT_OVER, net, net_input)
optimize(OPTIMIZER, p, closure, LR, num_iter)
PS:因笔记本性能原因,仅迭代到 2400 次
Iteration 00000 Loss 0.078438 PSNR_noisy: 11.054710 PSRN_gt: 11.548670 PSNR_gt_sm: 11.548670
Iteration 00100 Loss 0.018721 PSNR_noisy: 17.276807 PSRN_gt: 20.124856 PSNR_gt_sm: 16.768199
Iteration 02400 Loss 0.009465 PSNR_noisy: 20.238596 PSRN_gt: 30.566463 PSNR_gt_sm: 32.614663
待完成
- 由于设置参数部分的非普适性,想要测试更多的图片,仍需要时间调试
- 该项目中还有一些修复图像等模块,仍需要时间研究
正文完
发表至: 图像处理
2022-07-10